package com.laborder.sysyy.controller.announce;

import com.laborder.sysyy.entity.*;
import com.laborder.sysyy.service.annoncement.AnnounceService;
import com.laborder.sysyy.service.log.LogService;
import com.laborder.sysyy.service.user.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "announce")

public class AnnounceController {
	@Autowired
	private AnnounceService announceService;
	@Autowired
	private UserService userService;
	@Autowired
	private LogService logService;
     /**
       * @function: 分页信息回显
       * @author: 759057893@qq.com lyz
       * @date: 2019/1/16 15:20
       */
	@GetMapping("toAnnounce")
	public String toAnnounce(Model model){
		return "/announce/announceList";
	}
	 /**
	   * @function:  分页查询
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/16 15:20
	   */
	@ResponseBody
	@RequestMapping("/findAnnounceWithPage")
	public Map<String,Object> findAnnounceWithPage(int page,int limit,String aName){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("page",(page-1)*limit);
		map.put("limit",limit);
		map.put("aName",aName);
		map.put("user",user);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code",0);
		resultMap.put("msg","");
		resultMap.put("count",announceService.getAnnounceCount(map));
		resultMap.put("data",announceService.findAnnounceWithPage(map));
		return  resultMap;

	}
     /**
       * @function: 添加前信息回显
       * @author: 759057893@qq.com lyz
       * @date: 2019/1/16 15:19
       */
	@GetMapping("getAddAnnounce")
	public String getAddAnnounce(Model model){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");
		model.addAttribute(user);
		return "/announce/announceAdd";
	}
	 /**
	   * @function: 公告添加
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/16 15:18
	   */
	@ResponseBody
	@RequestMapping("/saveAnnounceInfo")
	public int saveAnnounceInfo(Announce announce) {
		int flag = 0;
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");
		announce.setuName(user.getUserName());
		announce.setaPostDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		announce.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		Announce a =announceService.getAnnounceInfoByName(announce.getaName(),announce.getaDesc());
		if(a!=null){
			flag = -1;
		}else if(a==null){
			flag = announceService.saveAnnounceInfo(announce);
		}else{
			flag = 0;
		}
		return flag;
	}

	/**
	 * @function:  公告信息查看
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:45
	 */
	@RequestMapping("announceDetail")
	public String announceDetail(Model model,int aId) {
	     Announce announce =announceService.getAnnounceByIdInfo(aId);
		model.addAttribute("announce", announce);
		return "announce/announceDetail";
	}

	/**
	 * @function:  学生信息修改前回显信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:45
	 */
	@RequestMapping("editAnnounce")
	public  String editAnnounce(Model model,int aId){
		Announce announce =announceService.getAnnounceByIdInfo(aId);
		model.addAttribute("announce", announce);
		return "announce/editAnnounce";
	}

	 /**
	   * @function:  公告信息修改
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/16 15:21
	   */
	@ResponseBody
	@RequestMapping("/updateAnnounceInfo")
	public boolean updateAnnounceInfo(Announce announce,int aId)  {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");
		announce.setuName(user.getUserName());
		Announce a =announceService.getAnnounceByIdInfo(aId);
		announce.setaPostDate(a.getaPostDate());
		announce.setaPostDate(announce.getaPostDate());
		announce.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		return announceService.updateAnnounceInfo(announce);
	}

	/**
	 * @function: 删除学生信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:12
	 */
	@ResponseBody
	@RequestMapping("/delAnnounceInfo")
	public  boolean delAnnounceInfo(int aId) {
		boolean flag = false;
		if(!flag){
			flag = announceService.delAnnounceInfo(aId);
		}
		return flag;
	}

	@ResponseBody
	@RequestMapping("/getAnnounceByIdInfo")
	public Announce getAnnounceByIdInfo(int aId) {
		return announceService.getAnnounceByIdInfo(aId);
	}
	
}